package com.jn.rxjava.presenter;

import android.content.Context;

import com.jn.rxjava.contract.TrackingContract2;
import com.jn.rxjava.entity.TrackingNum;
import com.jn.rxjava.http.GlobalRetrofitClient;
import com.jn.rxjava.service.TrackingService;
import com.jn.rxjava.utils.LogUtils;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.core.ObservableSource;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.functions.Function;
import io.reactivex.rxjava3.schedulers.Schedulers;
import okhttp3.OkHttpClient;

public class TrackingPresenter2 implements TrackingContract2.ITrackingPresenter {
    public static final int HTTP_SUCCESS = 0;
    public static final int HTTP_FAIL = 1;
    private TrackingContract2.ITrackingView mTrackingView;
    private Context mContext;
    private OkHttpClient mClient;

    public TrackingPresenter2(TrackingContract2.ITrackingView trackingView, Context context) {
        mTrackingView = trackingView;
        mContext = context;
        mClient = new OkHttpClient();
    }

    @Override
    public void getData(String trackingNumber) {//get 异步
        if (mTrackingView != null) {
            mTrackingView.onHttpLoading(true);
        }
        TrackingService service = GlobalRetrofitClient.getInstance().create(TrackingService.class);
        service.getData(trackingNumber)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                /*.map(new Function<TrackingNum, TrackingNum.DataDTO>() {//对数据进行类型转换
                    @Override
                    public TrackingNum.DataDTO apply(TrackingNum trackingNum) throws Throwable {
                        return trackingNum.getData();
                    }
                })
                .subscribe(new Observer<TrackingNum.DataDTO>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(TrackingNum.@NonNull DataDTO dataDTO) {
                        if (mTrackingView != null) {
                            mTrackingView.onHttpLoading(false);
                            mTrackingView.showData(dataDTO);
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        if (mTrackingView != null) {
                            mTrackingView.onHttpLoading(false);
                            mTrackingView.onHttpFail(e.getMessage());
                        }
                    }

                    @Override
                    public void onComplete() {

                    }
                });*/
                .flatMap(new Function<TrackingNum, ObservableSource<TrackingNum.DataDTO.TrackingDetailsDTO>>() {
                    @Override
                    public ObservableSource<TrackingNum.DataDTO.TrackingDetailsDTO> apply(TrackingNum trackingNum) throws Throwable {
                        return Observable.fromIterable(trackingNum.getData().getTrackingDetails());
                    }
                }).subscribe(new Observer<TrackingNum.DataDTO.TrackingDetailsDTO>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(TrackingNum.DataDTO.@NonNull TrackingDetailsDTO trackingDetailsDTO) {
                        LogUtils.d("asdf", "=====" + trackingDetailsDTO.toString());
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        if (mTrackingView != null) {
                            mTrackingView.onHttpLoading(false);
                        }
                    }

                    @Override
                    public void onComplete() {
                        if (mTrackingView != null) {
                            mTrackingView.onHttpLoading(false);
                        }
                    }
                });
    }
}
